package Metier.Controleurs;

import Donnees.DaoStat;
import Metier.Objets.Stat;
import Presentation.FenetrePrincipale;
import Presentation.Onglet;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

public class GestionStats {
    
    private final ResourceBundle configBundle;
    
    public GestionStats(Onglet onglet){
        if (onglet == null) {
            this.configBundle = ResourceBundle.getBundle("ressources/Main/Main", Locale.FRENCH);
        } else {
            this.configBundle = ResourceBundle.getBundle("ressources/Main/Main", onglet.getFP().getLocale());
        }
    }

    /**
     * 
     * @param tableModel
     * @param stats
     * @return 
     */
    private DefaultTableModel rempliTableModel(DefaultTableModel tableModel, HashSet<Stat> stats) {
        if (tableModel != null) {
            for (Iterator<Stat> it = stats.iterator(); it.hasNext();) {
                Object[] tmp = it.next().toVector();
                Object swap = tmp[1];
                tmp[1] = tmp[2];
                tmp[2] = swap;
                tmp[1] = tmp[1] == null ? "("+configBundle.getString("main.boisson")+")" : tmp[1];
                tableModel.addRow(tmp);
            }
        }

        return tableModel;
    }

    /**
     * 
     * @param rb
     * @return 
     */
    private DefaultTableModel getInitTableModel(ResourceBundle rb) {
        DefaultTableModel tableModel = new DefaultTableModel();

        //En-têtes des colonnes
        tableModel.addColumn(rb.getString("stats.premiereColonne"));
        tableModel.addColumn(rb.getString("stats.deuxiemeColonne"));
        tableModel.addColumn(rb.getString("stats.troisiemeColonne"));
        tableModel.addColumn(rb.getString("stats.quatriemeColonne"));

        return tableModel;
    }

    /**
     * 
     * @param cal
     * @param precision
     * @param rb
     * @return 
     */
    public TableModel getContenuDate(Calendar cal, int precision, ResourceBundle rb) {
        DefaultTableModel tableModel;
        tableModel = getInitTableModel(rb);
        try {
            tableModel = rempliTableModel(tableModel, DaoStat.getStatDate(cal, precision));
        } catch (SQLException e) {
            FenetrePrincipale.afficherPopupErreur(configBundle.getString("main.erreurStat"));
            e.printStackTrace();
        }
        return tableModel;
    }

}
